package pl.plesken.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;

import pl.plesken.tabs.MenuTab;
import pl.plesken.xsd.host.HostType;
import pl.plesken.xsd.host.HostsDocument;
import pl.plesken.xsd.host.HostsType;


public class HostsXML {
	
	LinkedList<MenuTab> row = new LinkedList<MenuTab>();
	LinkedList<MenuTab> save = new LinkedList<MenuTab>();
	StringBuffer str = new StringBuffer();
	StringBuffer xml = new StringBuffer();

	public LinkedList<MenuTab> getRow() {
		return row;
	}

	public void setRow(LinkedList<MenuTab> row) {
		this.row = row;
	}

	public LinkedList<MenuTab> getSave() {
		return save;
	}

	public void setSave(LinkedList<MenuTab> save) {
		this.save = save;
	}

	public void readHostValues() {
		try {
			File xmlFile = new File("conf/hosts.xml");
			HostsDocument hosts = HostsDocument.Factory.parse(xmlFile);
			HostType[] host = hosts.getHosts().getHostArray();
		
			row.clear();
			
			for (int i = 0; i < host.length; i++) {
				row.add(new MenuTab(host[i].getName(), host[i].getSrcpath(), host[i].getNmtpath(), host[i].getExclude(), host[i].getSign(), host[i].getStatus()));
				/*if (!host[i].getName().equals("")) {
					str.append(host[i].getName());
				} else {
					str.append("empty");
				}
				if (!host[i].getSrcpath().equals("")) {
					str.append("<BREAK>" + host[i].getSrcpath());
				} else {
					str.append("<BREAK>empty");
				}
				if (!host[i].getNmtpath().equals("")) {
					str.append("<BREAK>" + host[i].getNmtpath());
				} else {
					str.append("<BREAK>empty");
				}
				if (!host[i].getExclude().equals("")) {
					str.append("<BREAK>" + host[i].getExclude());
				} else {
					str.append("<BREAK>empty");
				}
				if (!host[i].getSign().equals("")) {
					str.append("<BREAK>" + host[i].getSign());
				} else {
					str.append("<BREAK>empty");
				}
				if (!host[i].getStatus().equals("")) {
					str.append("<BREAK>" + host[i].getStatus());
				} else {
					str.append("<BREAK>empty");
				}
				String val = str.toString();
				row.add(val);*/
				str.setLength(0);
			}
		} catch(XmlException e) {
			System.err.println(e);
		} catch(IOException e) {
			System.err.println(e);
		} catch(NullPointerException e) {
		}
	}
	
	
	/*public void saveHostValues() {
		try {
			FileOutputStream cfg = new FileOutputStream("conf/hosts.xml"); //$NON-NLS-1$
			BufferedWriter cfgxml = new BufferedWriter(new OutputStreamWriter(cfg));
			
			xml.setLength(0);
			
			xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
					"<hosts xmlns=\"http://host.xsd.plesken.pl\">\n");
			
			for (int i = 0; i < save.size(); i++) {
				String[] tab = save.get(i).split(";");
				if (!tab[0].toString().equals("null")) {
					xml.append("  <host>\n");
					if (tab[0].toString().equals("empty")) {
						xml.append("    <name></name>\n");
					} else {
						xml.append("    <name>" + tab[0].toString() + "</name>\n");
					}
					if (tab[1].toString().equals("empty")){
						xml.append("    <srcpath></srcpath>\n");
					} else {
						xml.append("    <srcpath>" + tab[1].toString() + "</srcpath>\n");
					}
					if (tab[2].toString().equals("empty")) {
						xml.append("    <nmtpath></nmtpath>\n");
					} else {
						xml.append("    <nmtpath>" + tab[2].toString() + "</nmtpath>\n");
					}
					if (tab[3].toString().equals("empty")) {
						xml.append("    <exclude></exclude>\n");
					} else {
						xml.append("    <exclude>" + tab[3].toString() + "</exclude>\n");
					}
					if (tab[4].toString().equals("empty")) {
						xml.append("    <sign></sign>\n");
					} else {
						xml.append("    <sign>" + tab[4].toString() + "</sign>\n");
					}
					if (tab[5].toString().equals("empty")) {
						xml.append("    <status></status>\n");
					} else {
						xml.append("    <status>" + tab[5].toString() + "</status>\n");
					}
					xml.append("  </host>\n");
				}
			}

			xml.append("</hosts>\n");
			
			cfgxml.write(xml.toString());
			cfgxml.close();
			save.clear();

		} catch(FileNotFoundException e) {
			
		} catch(IOException e) {
			
		} catch(NoSuchElementException e) {
			
		}
	}*/
	
	public void saveHostValues() {
		try {
			HostsDocument hosts = HostsDocument.Factory.newInstance();
			HostsType hostsType = HostsType.Factory.newInstance();
		
			for (int i = 0; i < save.size(); i++) {
				//String[] tab = save.get(i).split(";");
			
				//if (!tab[0].toString().equalsIgnoreCase("null")) {
				hostsType.addNewHost();
				HostType hostType = HostType.Factory.newInstance();
				hostType.setName(save.get(i).getName());
				hostType.setSrcpath(save.get(i).getSrc());
				hostType.setNmtpath(save.get(i).getNmt());
				hostType.setExclude(save.get(i).getExcl());
				hostType.setSign(save.get(i).getSign());
				hostType.setStatus(save.get(i).getStat());
				hostsType.setHostArray(i, hostType);
				//}
			}
		
			hosts.setHosts(hostsType);
		
			save.clear();
		
			File out = new File("conf/hosts.xml");
			OutputStream os = new FileOutputStream(out);
		
			XmlOptions options = new XmlOptions();
			options.setSavePrettyPrint();
			hosts.save(out, options);
			os.flush();
		
		} catch(IOException e) {
			System.err.println(e);
		} catch(NullPointerException e) {
		}
	}
	
}
